//
// Created by 77873 on 2020/10/12 0012.
//

#ifndef FFMPEGVIDEOPLAYER_VIDEODECODEC_H
#define FFMPEGVIDEOPLAYER_VIDEODECODEC_H

#include "CallBackToJava.h"
#include "AudioDecodec.h"

extern "C" {
#include <libavcodec/avcodec.h>
#include <libavutil/time.h>

#include <libswscale/swscale.h>
#include <libavutil/imgutils.h>
};

#define CODEC_YUV 0
#define CODEC_MEDIACODEC 1


class VideoDecodec {
public://视频
    int videoStreamsIndex = -1;
    AVCodecParameters *videoCodecParameters = NULL;
    AVCodecContext *videoCodecContex = NULL;

    CallBackToJava *callBackToJava = NULL;
    AVRational videoBaseTime;

    double clock = 0;
    //fps
    int fps_value = 0;

    double delayTime = 0;
    double defaultDelayTime = 0.04;

    //使用线程获取到解码数据
    pthread_t thread_play;

    //音视频同步   视频向 音频同步
    AudioDecodec *audioDecodec = NULL;

    //视频数据avpacket
    DataQueue *videoDataQueue;
    PlayStatus *playStatus = NULL;

    //解码线程锁
    pthread_mutex_t codecMutex;

    int codecType = CODEC_YUV;

    /**
     * 硬解码上下文对象
     */
    AVBSFContext *avbsfContext = NULL;
public:
    VideoDecodec(CallBackToJava *callBackToJava, PlayStatus *ps);

    ~VideoDecodec();

    void startPlay();

    void release();


    double getFrameDiffTime(AVFrame *avFrame, AVPacket *avPacket);

    double getDelayeTime(double diff);
};


#endif //FFMPEGVIDEOPLAYER_VIDEODECODEC_H
